package com.icw.basis;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import lombok.Getter;

public abstract class PureGeneric<T> {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PureGeneric() {
		// 1. entityClass
		Type genType = getClass();
		while (Class.class.isInstance(genType) && null != (genType = ((Class<?>) genType).getGenericSuperclass())) {
			if (ParameterizedType.class.isInstance(genType)) {
				Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
				if (params.length > 0) {
					if (Class.class.isInstance(params[0])) {
						entityClass = (Class) params[0];
					}
				}
				break;
			}
		}
	}

	@Getter
	private Class<T> entityClass;

	public void bindEntityClass(Class<T> entityClass) throws Exception {
		if (this.entityClass == null) {
			this.entityClass = entityClass;
		} else {
			throw new Exception("Entity class had already been bound.");
		}
	}
}
